/* eslint-disable import/prefer-default-export */
/**
 * 覆盖组件的 props
 */
export const modifyProps = (field, props, fields) => {
    if (!fields || fields.length === 0) return fields;
    const findIndex = fields.findIndex(item => item.id === field);

    if (findIndex !== -1) {
        const current = fields[findIndex];
        return [
            ...fields.slice(0, findIndex),
            {
                ...current,
                params: current.params
                    ? {
                        ...current.params,
                        component: current.params.component
                            ? {
                                ...current.params.component,
                                props: {
                                    ...current.params.component.props,
                                    ...props
                                }
                            }
                            : {
                                props
                            }
                    }
                    : {
                        component: {
                            props
                        }
                    }
            },
            ...fields.slice(findIndex + 1)
        ];
    }
    return fields;
};

/**
 * 覆盖组件的 config
 */
export const modifyConfig = (field, config, fields) => {
    const findIndex = fields.findIndex(item => item.id === field);

    if (findIndex !== -1) {
        const current = fields[findIndex];
        return [
            ...fields.slice(0, findIndex),
            {
                ...current,
                params: current.params
                    ? {
                        ...current.params,
                        component: current.params.component
                            ? {
                                ...current.params.component,
                                config: {
                                    ...current.params.component.config,
                                    ...config
                                }
                            }
                            : {
                                config
                            }
                    }
                    : {
                        component: {
                            config
                        }
                    }
            },
            ...fields.slice(findIndex + 1)
        ];
    }
    return fields;
};

/**
 * 覆盖组件的 itemProps
 */
export const modifyItemProps = (field, itemProps, fields) => {
    const findIndex = fields.findIndex(item => item.id === field);

    if (findIndex !== -1) {
        const current = fields[findIndex];
        return [
            ...fields.slice(0, findIndex),
            {
                ...current,
                params: current.params
                    ? {
                        ...current.params,
                        itemProps: current.params.itemProps
                            ? {
                                ...current.params.itemProps,
                                ...itemProps
                            }
                            : itemProps
                    }
                    : {
                        itemProps
                    }
            },
            ...fields.slice(findIndex + 1)
        ];
    }
    return fields;
};

/**
 * 覆盖组件的 component
 */
export const modifyComponent = (field, config, fields) => {
    const findIndex = fields.findIndex(item => item.id === field);

    if (findIndex !== -1) {
        const current = fields[findIndex];
        return [
            ...fields.slice(0, findIndex),
            {
                ...current,
                params: current.params
                    ? {
                        ...current.params,
                        component: current.params.component
                            ? {
                                ...current.params.component,
                                ...config
                            }
                            : {
                                config
                            }
                    }
                    : {
                        component: {
                            config
                        }
                    }
            },
            ...fields.slice(findIndex + 1)
        ];
    }
    return fields;
};

/**
 * 覆盖组件的 params
 */
export const modifyParams = (field, config, fields) => {
    const findIndex = fields.findIndex(item => item.id === field);

    if (findIndex !== -1) {
        const current = fields[findIndex];
        return [
            ...fields.slice(0, findIndex),
            {
                ...current,
                params: current.params
                    ? {
                        ...current.params,
                        ...config
                    }
                    : config
            },
            ...fields.slice(findIndex + 1)
        ];
    }
    return fields;
};


// export const removeFields = (fieldNames, fields) => {
//     const tmpFields = [...fields];

//     fieldNames.map(field => {
//         const findIndex = fields.findIndex(item => item.id === field);

//         if (findIndex !== -1) {
//             tmpFields.splice(findIndex, 1);
//         }
//     });

//     return tmpFields;
// };
